package com.ymatou.iapi.dguser.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.dguser.parameter.DeleteUserSelfBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.DeleteUserSelfCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGUserInfo;
/**
*删除用户
*FreeMaker Template自动生成代码
*/
import com.ymttest.database.sqlwapper.DGUserWapper;
@TestSuite
public class Ts_DeleteUserSelf{
	private static DeleteUserSelfBean DeleteUserSelfBean;
	private static DeleteUserSelfCall DeleteUserSelfCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除用户");
	}

	@Before
	public void caseUp() {
		DeleteUserSelfBean=new DeleteUserSelfBean();
		DeleteUserSelfCall=new DeleteUserSelfCall();
		DGUserWapper= new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(DeleteUserSelfCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer userId) {
		List<DGUserInfo> info = DGUserWapper.selectDGUserInfo(userId);
		Logger.verifyEquals(2, info.get(0).getUserStatus(), "逻辑删除");
		Logger.verifyEquals(0,DGUserWapper.selectDGThirdPartyBindInfo(userId).size(),"第三方解绑");
		Logger.verifyEquals(0,DGUserWapper.selectDGWxUnionidBindInfo(userId).size(),"第三方解绑Unionid");
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteUserSelf_001() {
		Logger.start(true,"删除用户,成功-默认用户");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			String AccessToken = AuthCallServiceV2.createToken(userId, DGUserCallService.DG_BuyerWxApp.toString());
			Map<String, Object> info = AuthCallServiceV2.signAuth(AccessToken);
			Logger.comment(info.toString());
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("true", DeleteUserSelfCall.getString("success"), "验证success");
			verifyDB(userId);
			
			for (int i = 0; i < 15; i++) {
				info=AuthCallServiceV2.signAuth(AccessToken);
				Logger.comment(info.toString());
				if(!Boolean.valueOf(info.get("success").toString())) {
					break;
				}
				Thread.sleep(1000);
			}
			Logger.verifyEquals(false, info.get("success"), "验证success");
			MessageListBean MessageListBean = MqQueryCallService.initDeleteGodUser(userId);
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			Logger.verifyEquals(true, joa.size()>=1, "验证发送总线消息");
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_002() {
		Logger.start(true,"删除用户,成功-默认商家");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initSellerUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("true", DeleteUserSelfCall.getString("success"), "验证success");
			verifyDB(userId);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_003() {
		Logger.start(true,"删除用户,成功-默认客服");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("true", DeleteUserSelfCall.getString("success"), "验证success");
			verifyDB(userId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_004() {
		Logger.start(true,"删除用户,成功-独立用户");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setAppSource(DGUserCallService.DG_SellerWxApp);
			usercreateBean.setWxAppName(DGUserCallService.TestWxsqOther);
			usercreateBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("true", DeleteUserSelfCall.getString("success"), "验证success");
			verifyDB(userId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_005() {
		Logger.start(true,"不输入uid失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			int userId=1;
//			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("false", DeleteUserSelfCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_006() {
		Logger.start(true,"不输入UserType失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			int userId=1;
			DeleteUserSelfBean.setUserId(userId);
//			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("false", DeleteUserSelfCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_007() {
		Logger.start(true,"不输入Nickname失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			int userId=1;
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
//			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("false", DeleteUserSelfCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_008() {
		Logger.start(true,"删除用户,usertype不匹配失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initSellerUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType()+1);
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname());
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("false", DeleteUserSelfCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_DeleteUserSelf_009() {
		Logger.start(true,"删除用户,Nickname不匹配失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initSellerUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			DeleteUserSelfBean.setUserId(userId);
			DeleteUserSelfBean.setUserType(usercreateBean.getUserType());
			DeleteUserSelfBean.setNickName(usercreateBean.getNickname()+1);
			DeleteUserSelfCall.setData(DeleteUserSelfBean);
			DeleteUserSelfCall.callService();			
			Logger.verifyEquals("false", DeleteUserSelfCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}